package cn.hbwe.service.impl;

import cn.hbwe.Dto.pLoginDTO;
import cn.hbwe.Dto.uLoginDTO;
import cn.hbwe.pojo.entity.Patient;
import cn.hbwe.pojo.entity.User;
import cn.hbwe.mapper.UserMapper;
import cn.hbwe.service.IUserService;
import cn.hbwe.util.CustomException;
import cn.hbwe.util.constant.MessageConstant;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 7组
 * @since 2023-10-18
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Override
    public User ulogin(uLoginDTO uloginDTO) {
        //条件构造器
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getUserAccount, uloginDTO.getUserAccount());
        lqw.orderByDesc(User::getUserId);

        //查询
       User user = this.getOne(lqw);

        //是否查询到此用户
        if (user == null) {
            throw new CustomException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        //md5密码加密
        String password = uloginDTO.getUserPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        //密码是否正确
        if (!user.getUserPassword().equals(password)) {
            throw new CustomException(MessageConstant.PASSWORD_ERROR);
        }

        //返回结果
        return user;
    }




}
